﻿using System;
using System.Data.Objects;
using System.Web.UI.WebControls;
using Commons;
using DAL;
using DAL.Enums;
using NUnit.Framework;
using SimpComm;
using ViewModel;
using System.Collections.Generic;
using System.Linq.Dynamic;
using System.Linq;

namespace Bll
{
    public class AdmColService : BaseService<AdmCol>
    {
        private static readonly AdmColService instance = new AdmColService();

        static AdmColService()
        {
        }

        private AdmColService()
        {
        }

        public static AdmColService Instance
        {
            get { return instance; }
        }


        public static string Get(string name, string descript = "", byte classLevel = 0, int admColID = 0, string url = "")
        {
            var admPowersService = AdmColService.Instance;
            var admPowers = admPowersService.Dal.GetFirstEntity(p => p.Name == name,null);
            if (admPowers != null)
                return admPowers.Key;
            else//不存在权限,添加
            {
                var powers = new AdmColUpdateModel();
                powers.Name = name;
                if (descript.NotNullOrWhiteSpace())
                    powers.Descript = descript;
                powers.ClassLevel = classLevel;
                powers.AdmColID = admColID;
                powers.Url = url;
                var msg = admPowersService.Insert(powers);
                if (msg.Success)
                {
                    return msg.Result;
                }
                else
                {
                    return "none";
                }
            }
        }


        public MsgBox<string> Insert(AdmColUpdateModel addModel)
        {
            if (addModel.Name.IsNullOrWhiteSpace())
                return new MsgBox<string>(false, "请输入必须值.");


            if (addModel.ID > 0)
            {
                var msg = base.Update(p => p.ID == addModel.ID, p => new AdmCol()
                                                                  {
                                                                      Descript = addModel.Descript,
                                                                      Name = addModel.Name,
                                                                      Url = addModel.Url ?? "#",
                                                                      SmallClass = addModel.SmallClass,
                                                                      ClassLevel = addModel.ClassLevel,
                                                                      AdmColID = addModel.AdmColID,
                                                                      OrderBy = addModel.OrderBy,
                                                                      IsOpenWin = addModel.IsOpenWin,
                                                                  });
                var aAdmCol = Dal.GetEntity(p => p.ID == addModel.ID);
                return new MsgBox<string>()
                           {
                               Success = msg.Success,
                               Message = msg.Message,
                               Result = aAdmCol.Key,
                           }; ;
            }
            else
            {
                var user = new AdmCol()
                               {
                                   Key = Commons.Randoms.GetRandomChars(12),
                                   Descript = addModel.Descript,
                                   Name = addModel.Name,
                                   Url = addModel.Url ?? "#",
                                   SmallClass = addModel.SmallClass,
                                   ClassLevel = addModel.ClassLevel,
                                   AdmColID = addModel.AdmColID,
                                   OrderBy = addModel.OrderBy,
                                   IsOpenWin = addModel.IsOpenWin,
                               };
                //            Action<AdmCol> defaultValueFun = null;
                MsgBox msg = base.Insert(user, null);
                return new MsgBox<string>()
                {
                    Success = msg.Success,
                    Message = msg.Message,
                    Result = user.Key,
                };
            }
        }

        public DataInfo<AdmColUpdateModel> GetEditView(Func<AdmCol, bool> func)
        {
            Func<AdmCol, AdmColUpdateModel> select = addModel =>
                                                         {
                                                             return new AdmColUpdateModel()
                                                                        {
                                                                            Descript = addModel.Descript,
                                                                            Name = addModel.Name,
                                                                            Url = addModel.Url,
                                                                            SmallClass = addModel.SmallClass,
                                                                            ClassLevel = addModel.ClassLevel,
                                                                            AdmColID = addModel.AdmColID,
                                                                            OrderBy = addModel.OrderBy,
                                                                            IsOpenWin = addModel.IsOpenWin,
                                                                        };
                                                         };
            return base.GetModel(func, select);
        }

        public MD<AdmColListModel> SM_List(SMP<AdmCol, AdmColListModel> s)
        {
            s.
                getmodel = p =>
                             {
                                 return new AdmColListModel()
                                            {
                                                ID = p.ID,
                                                Name = p.Name,
                                                Url = p.Url,
                                                SmallClass = p.SmallClass,
                                            };
                             };
            return base.SM(s);
        }


    }


#if DEBUG
    [TestFixture]
    public class TestAdmColService
    {
        //        [Test]
        //        public void 测试批量1update()
        //        {
        //            using (PizzaDataDataContext db=new PizzaDataDataContext())
        //            {
        //                var entsnLs=new List<int>(){1,2,3,4};
        //                var rows = db.AdmCol.UpdateEntity(p => , p => new AdmCol() {Url = "@000"});
        //            }
        //        }
    }
#endif
}